<?php
class User_account_model extends BZ_Model
{
    protected $table = 'user_account';
    
    public function findByUid($uid)
    {
        $this->db->where('uid', $uid);
        return $this->db->get($this->table);
    }
    
    public function updateUserAcount($uid, $account=array())
    {
        $data = array();
        if (!empty($account['amount_month'])) {
            $this->db->set('amount_month', 'amount_month-'.$account['amount_month'], FALSE);
        }
        if (!empty($account['amount_carry'])) {
            $this->db->set('amount_carry', 'amount_carry+'.$account['amount_carry'], FALSE);
        }
        if (!empty($account['amount_bonus'])) {
            $this->db->set('amount_bonus', 'amount_bonus+'.$account['amount_bonus'], FALSE);
        }
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }

    public function updateUserAccount($uid, $account=array())
    {
        $data = array();
        if (isset($account['n_amount_carry'])) {
            $data['amount_carry'] = $account['n_amount_carry'];
        }
        if (isset($account['n_amount_month'])) {
            $data['amount_month'] = $account['n_amount_month'];
        }
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 订单分润
     * @param unknown $orderProfit
     */
    public function updateUserAccountProfit($account_name,$total_name,$totalAmount,$item)
    {
        $this->db->set($account_name, "$account_name+".$item->account, FALSE);
        $this->db->set($total_name, "$total_name+".$totalAmount, FALSE);
        $this->db->where('uid', $item->uid);
        $this->db->limit(1);
        return $this->db->update($this->table);
    }
    
    public function updateUserAccountFreight($uid, $amount)
    {
        $this->db->set('amount_carry', "amount_carry+".$amount, FALSE);
        $this->db->set('income_total', "income_total+".$amount, FALSE);
        $this->db->where('uid', $uid);
        $this->db->limit(1);
        return $this->db->update($this->table);
    }
}